package org.lingshi.shopping_manager_api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.lingshi.common.entity.Admin;
import org.lingshi.common.result.BaseResult;
import org.lingshi.common.service.IAdminService;
import org.lingshi.shopping_manager_api.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @DubboReference
    private IAdminService adminService;

    @Autowired
    private ILoginService loginService;

    /**
     * 新增
     *
     * @param admin 管理员
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Admin admin) {
        adminService.add(admin);
        return BaseResult.success();
    }

    /**
     * 修改
     *
     * @param admin
     * @return
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Admin admin) {
        adminService.update(admin);
        return BaseResult.success();
    }

    /**
     * 删除
     *
     * @param aid
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long aid) {
        adminService.deleteById(aid);
        return BaseResult.success();
    }

    /**
     * 根据id查询
     *
     * @param aid
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(long aid) {
        Admin admin = adminService.findById(aid);
        return BaseResult.success(admin);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search")
    @SaCheckPermission("/admin/search")
    public BaseResult search(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<Admin> search = adminService.search(page, size);
        return BaseResult.success(search);
    }

    @PostMapping("/updateRoleToAdmin")
    @SaCheckPermission("/admin/updateRoleToAdmin")
    public BaseResult updateroleToAdmin(Long aid,Long[] rids){
        adminService.updateRoleToAdmin(aid,rids);
        return BaseResult.success();
    }

    @PostMapping("/login")
    public BaseResult login(String username,String password){
        return loginService.login(username,password);
    }

    @PostMapping("/logout")
    public BaseResult logout(){
        StpUtil.logout();
        return BaseResult.success();
    }
}
